using System.Collections;
using UnityEngine;

public class ChestDev : MonoBehaviour
{
	public GotchaDev gotchaDev;

	public GotchaChestType chestType;

	private GotchaChest gotchaChest;

	public new UILabel name;

	public UILabel count;

	public UITexture texture;

	public UILabel openTime;

	public UILabel addCount;

	private GotchaReward resultReward;

	private void Awake()
	{
		Init();
	}

	private void OnEnable()
	{
		SetDevUI();
	}

	public void Init()
	{
		switch (chestType)
		{
		case GotchaChestType.Wooden:
			gotchaChest = GotchaManager.instance.WoodenChest;
			break;
		case GotchaChestType.Bronze:
			gotchaChest = GotchaManager.instance.BronzeChest;
			break;
		case GotchaChestType.Silvern:
			gotchaChest = GotchaManager.instance.SilvernChest;
			break;
		case GotchaChestType.Golden:
			gotchaChest = GotchaManager.instance.GoldenChest;
			break;
		case GotchaChestType.Non:
			Debug.LogError("GotchaChestType is Non!");
			gotchaChest.type = GotchaChestType.Non;
			break;
		}
		name.text = gotchaChest.type.ToString();
		texture.mainTexture = Resources.Load<Texture>("AtlasShopGuns/Closed_Сhest_" + gotchaChest.type);
		SetDevUI();
	}

	public void SetDevUI()
	{
		count.text = gotchaChest.count.ToString();
		openTime.text = gotchaChest.secondsToOpen.ToString();
	}

	public void AddChest()
	{
		int result = 0;
		if (int.TryParse(addCount.text, out result))
		{
			gotchaChest.Add(result);
		}
		SetDevUI();
	}

	public void RemoveAll()
	{
		gotchaChest.count = 0;
		SetDevUI();
	}

	public void SetOpenTime()
	{
		int result = 0;
		if (int.TryParse(openTime.text, out result))
		{
			gotchaChest.secondsToOpen = result;
		}
		SetDevUI();
	}

	public void Simulate()
	{
		CoroutineRunner.Instance.StartCoroutine(SimulationIEnumerator());
	}

	private IEnumerator SimulationIEnumerator()
	{
		gotchaDev.Log(string.Concat("----------------------------  ", gotchaChest.type, "  Cest   Simulation   Results  ----------------------------"), Color.yellow);
		int iteration = 1;
		while (gotchaChest.count > 1)
		{
			resultReward = GotchaManager.instance.GetSpinReward(gotchaChest.GetChestRewards(), gotchaChest);
			gotchaChest.Open();
			if (resultReward == null)
			{
				gotchaDev.Log("No result reward!", Color.red);
				Debug.LogError("No result reward!");
				yield break;
			}
			if (resultReward.cost >= GotchaManager.instance.costFrontier)
			{
				gotchaDev.Log(iteration + "  |  " + resultReward.id + "  |   Cost:  " + resultReward.cost + "  |   Money box:  " + gotchaChest.moneyBox, Color.cyan);
			}
			else
			{
				gotchaDev.Log(iteration + "  |  " + resultReward.id + "  |   Cost:  " + resultReward.cost + "  |   Money box:  " + gotchaChest.moneyBox, Color.white);
			}
			resultReward.GiveReward();
			gotchaChest.ClearActualRewards();
			SetDevUI();
			iteration++;
			yield return null;
		}
		gotchaDev.Log("--------------------------------------------------------------------------------------------------", Color.yellow);
	}
}
